<template>
  <a-modal
    title='设置提现账号'
    :width='720'
    :visible='visible'
    :confirmLoading='confirmLoading'
    :maskClosable='false'
    @ok='handleSubmit'
    @cancel='handleCancel'
  >
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form' @submit='handleSubmit' :selfUpdate='true'>
        <a-form-item label='收款账号' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-input v-decorator="['account', {rules: [{required: true, message: '卡号'}]}]" />
        </a-form-item>
        <a-form-item label='收款主体' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-input v-decorator="['rname', {rules: [{required: true, message: '公户填写企业名称个人填写姓名'}]}]" />
        </a-form-item>
        <a-form-item label='开户行' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-input v-decorator="['bank', {rules: [{required: true, message: '具体开户行'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/store/address'
import { SelectRegion, SelectImage } from '@/components'
import pick from 'lodash.pick'

export default {
  components: {
    SelectRegion,
    SelectImage
  },
  data() {
    return {
      url: null,
      // 对话框标题
      title: '设置提现账号',
      // 标签布局属性
      labelCol: { span: 3 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this)
    }
  },
  created() {

  },
  mounted() {

  },
  methods: {
    /**
     * 显示对话框
     */
    add() {
      Api.binfo()
        .then((res) => {
          const record = res.data
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(record, ['account', 'bank', 'rname']))
          })
        })

      // 显示窗口
      this.visible = true
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      Api.wd({ form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
